home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / fontutil.6 / fontutil / fontutils-0.6 / limn / curve.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-05  |  7.5 KB  |  304 lines

  1. /* curve.c: operations on the lists of pixels and lists of curves.
  2.  
  3. Copyright (C) 1992 Free Software Foundation, Inc.
  4.  
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "config.h"
  20.  
  21. #include "logreport.h"
  22.  
  23. #include "curve.h"
  24. #include "display.h"
  25.  
  26.  
  27. /* Return an entirely empty curve.  */
  28.  
  29. curve_type
  30. new_curve ()
  31. {
  32.   curve_type curve = xmalloc (sizeof (struct curve));
  33.  
  34.   curve->point_list = NULL;
  35.   CURVE_LENGTH (curve) = 0;
  36.   CURVE_CYCLIC (curve) = false;
  37.   CURVE_START_TANGENT (curve) = CURVE_END_TANGENT (curve) = NULL;
  38.   PREVIOUS_CURVE (curve) = NEXT_CURVE (curve) = NULL;
  39.  
  40.   return curve;
  41. }
  42.  
  43.  
  44. /* Start the returned curve off with COORD as the first point.  */
  45.  
  46. curve_type
  47. init_curve (coordinate_type coord)
  48. {
  49.   curve_type curve = new_curve ();
  50.  
  51.   curve->point_list = xmalloc (sizeof (point_type));
  52.   CURVE_LENGTH (curve) = 1;
  53.  
  54.   CURVE_POINT (curve, 0) = int_to_real_coord (coord);
  55.  
  56.   return curve;
  57. }
  58.  
  59.  
  60. /* Don't copy the points or tangents, but copy everything else.  */
  61.  
  62. curve_type
  63. copy_most_of_curve (curve_type old_curve)
  64. {
  65.   curve_type curve = new_curve ();
  66.  
  67.   CURVE_CYCLIC (curve) = CURVE_CYCLIC (old_curve);
  68.   PREVIOUS_CURVE (curve) = PREVIOUS_CURVE (old_curve);
  69.   NEXT_CURVE (curve) = NEXT_CURVE (old_curve);
  70.  
  71.   return curve;
  72. }
  73.  
  74.  
  75. /* The length of CURVE will be zero if we ended up not being able to fit
  76.    it (which in turn implies a problem elsewhere in the program, but at
  77.    any rate, we shouldn't try here to free the nonexistent curve).  */
  78.  
  79. void
  80. free_curve (curve_type curve)
  81. {
  82.   if (CURVE_LENGTH (curve) > 0)
  83.     safe_free ((address *) &(curve->point_list));
  84. }
  85.  
  86.  
  87. void
  88. append_pixel (curve_type curve, coordinate_type coord)
  89. {
  90.   append_point (curve, int_to_real_coord (coord));
  91. }
  92.  
  93.  
  94. void
  95. append_point (curve_type curve, real_coordinate_type coord)
  96. {
  97.   CURVE_LENGTH (curve)++;
  98.   XRETALLOC (curve->point_list, CURVE_LENGTH (curve), point_type);
  99.   LAST_CURVE_POINT (curve) = coord;
  100.   /* The t value does not need to be set.  */
  101. }
  102.  
  103. /* Show the pixels of the curve online.  */
  104.  
  105. void
  106. display_curve (curve_type c)
  107. {
  108.   unsigned this_point;
  109.  
  110.   if (!wants_display) return;
  111.   
  112.   for (this_point = 0; this_point < CURVE_LENGTH (c); this_point++)
  113.     display_pixel (CURVE_POINT (c, this_point));
  114. }
  115.  
  116.  
  117. /* Print a curve in human-readable form.  It turns out we never care
  118.    about most of the points on the curve, and so it is pointless to
  119.    print them all out umpteen times.  What matters is that we have some
  120.    from the end and some from the beginning.  */
  121.  
  122. #define NUM_TO_PRINT 3
  123.  
  124. #define LOG_CURVE_POINT(c, p, print_t)                    \
  125.   do                                    \
  126.     {                                    \
  127.       LOG2 ("(%.3f,%.3f)", CURVE_POINT (c, p).x, CURVE_POINT (c, p).y);    \
  128.       if (print_t)                            \
  129.         LOG1 ("/%.2f", CURVE_T (c, p));                    \
  130.     }                                    \
  131.   while (0)
  132.  
  133. void
  134. log_curve (curve_type curve, boolean print_t)
  135. {
  136.   unsigned this_point;
  137.   
  138.   if (!logging) return;
  139.  
  140.   LOG1 ("curve id = %x:\n", (unsigned) curve);
  141.   LOG1 ("  length = %u.\n", CURVE_LENGTH (curve));
  142.   if (CURVE_CYCLIC (curve))
  143.     LOG ("  cyclic.\n");
  144.  
  145.   /* It should suffice to check just one of the tangents for being null
  146.      -- either they both should be, or neither should be.  */
  147.   if (CURVE_START_TANGENT (curve) != NULL)
  148.     LOG4 ("  tangents = (%.3f,%.3f) & (%.3f,%.3f).\n",
  149.           CURVE_START_TANGENT (curve)->dx, CURVE_START_TANGENT (curve)->dy,
  150.           CURVE_END_TANGENT (curve)->dx, CURVE_END_TANGENT (curve)->dy);
  151.  
  152.   LOG ("  ");
  153.   
  154.   /* If the curve is short enough, don't use ellipses.  */
  155.   if (CURVE_LENGTH (curve) <= NUM_TO_PRINT * 2)
  156.     {
  157.       for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
  158.         {
  159.           LOG_CURVE_POINT (curve, this_point, print_t);
  160.           LOG (" ");
  161.  
  162.           if (this_point != CURVE_LENGTH (curve) - 1
  163.               && (this_point + 1) % NUM_TO_PRINT == 0)
  164.             LOG ("\n  ");
  165.         }
  166.     }
  167.   else
  168.     {
  169.       for (this_point = 0;
  170.            this_point < NUM_TO_PRINT && this_point < CURVE_LENGTH (curve);
  171.            this_point++)
  172.         {
  173.           LOG_CURVE_POINT (curve, this_point, print_t);
  174.           LOG (" ");
  175.         }
  176.  
  177.       LOG ("...\n   ...");
  178.  
  179.       for (this_point = CURVE_LENGTH (curve) - NUM_TO_PRINT;
  180.            this_point < CURVE_LENGTH (curve);
  181.            this_point++)
  182.         {
  183.           LOG (" ");
  184.           LOG_CURVE_POINT (curve, this_point, print_t);
  185.         }
  186.     }
  187.  
  188.   LOG (".\n");
  189. }
  190.  
  191.  
  192. /* Like `log_curve', but write the whole thing.  */
  193.  
  194. void
  195. log_entire_curve (curve_type curve)
  196. {
  197.   unsigned this_point;
  198.   
  199.   if (!logging) return;
  200.  
  201.   LOG1 ("curve id = %x:\n", (unsigned) curve);
  202.   LOG1 ("  length = %u.\n", CURVE_LENGTH (curve));
  203.   if (CURVE_CYCLIC (curve))
  204.     LOG ("  cyclic.\n");
  205.  
  206.   /* It should suffice to check just one of the tangents for being null
  207.      -- either they both should be, or neither should be.  */
  208.   if (CURVE_START_TANGENT (curve) != NULL)
  209.     LOG4 ("  tangents = (%.3f,%.3f) & (%.3f,%.3f).\n",
  210.           CURVE_START_TANGENT (curve)->dx, CURVE_START_TANGENT (curve)->dy,
  211.           CURVE_END_TANGENT (curve)->dx, CURVE_END_TANGENT (curve)->dy);
  212.  
  213.   LOG (" ");
  214.  
  215.   for (this_point = 0; this_point < CURVE_LENGTH (curve); this_point++)
  216.     {
  217.       LOG (" ");
  218.       LOG_CURVE_POINT (curve, this_point, true);
  219.     }
  220.  
  221.   LOG (".\n");
  222. }
  223.  
  224. /* Return an initialized but empty curve list.  */
  225.  
  226. curve_list_type
  227. new_curve_list ()
  228. {
  229.   curve_list_type curve_list;
  230.  
  231.   curve_list.length = 0;
  232.   curve_list.data = NULL;
  233.  
  234.   return curve_list;
  235. }
  236.  
  237.  
  238. /* Free a curve list and all the curves it contains.  */
  239.  
  240. void
  241. free_curve_list (curve_list_type *curve_list)
  242. {
  243.   unsigned this_curve;
  244.  
  245.   for (this_curve = 0; this_curve < curve_list->length; this_curve++)
  246.     free_curve (curve_list->data[this_curve]);
  247.  
  248.   /* If the character was empty, it won't have any curves.  */
  249.   if (curve_list->data != NULL)
  250.     safe_free ((address *) &(curve_list->data));
  251. }
  252.  
  253.  
  254. /* Add an element to a curve list.  */
  255.  
  256. void
  257. append_curve (curve_list_type *curve_list, curve_type curve)
  258. {
  259.   curve_list->length++;
  260.   XRETALLOC (curve_list->data, curve_list->length, curve_type);
  261.   curve_list->data[curve_list->length - 1] = curve;
  262. }
  263.  
  264. /* Return an initialized but empty curve list array.  */
  265.  
  266. curve_list_array_type
  267. new_curve_list_array ()
  268. {
  269.   curve_list_array_type curve_list_array;
  270.  
  271.   CURVE_LIST_ARRAY_LENGTH (curve_list_array) = 0;
  272.   curve_list_array.data = NULL;
  273.  
  274.   return curve_list_array;
  275. }
  276.  
  277.  
  278. /* Free a curve list array and all the curve lists it contains.  */
  279.  
  280. void
  281. free_curve_list_array (curve_list_array_type *curve_list_array)
  282. {
  283.   unsigned this_list;
  284.  
  285.   for (this_list = 0; this_list < CURVE_LIST_ARRAY_LENGTH (*curve_list_array);
  286.        this_list++) 
  287.     free_curve_list (&CURVE_LIST_ARRAY_ELT (*curve_list_array, this_list));
  288.  
  289.   /* If the character was empty, it won't have any curves.  */
  290.   if (curve_list_array->data != NULL)
  291.     safe_free ((address *) &(curve_list_array->data));
  292. }
  293.  
  294.  
  295. /* Add an element to a curve list array.  */
  296.  
  297. void
  298. append_curve_list (curve_list_array_type *l, curve_list_type curve_list)
  299. {
  300.   CURVE_LIST_ARRAY_LENGTH (*l)++;
  301.   XRETALLOC (l->data, CURVE_LIST_ARRAY_LENGTH (*l), curve_list_type);
  302.   LAST_CURVE_LIST_ARRAY_ELT (*l) = curve_list;
  303. }
  304.